
app.controller('profitCtrl', ['$scope', function ($scope) {
	var needNewPagination = true;
	
    refreshData(1);
    
    AQ.callAQ("refreshUserInfo.do", null, function(result) {
		var User = result[1];
		$.cookie("userName", User.userName, {expires: 30});
		$.cookie("User", JSON.stringify(User));
		$scope.$apply(function () {			
			$scope.recommendCutRate = parseInt(User.recommendCutRate * 100) + "%";
			$scope.coopBoxShow = User.recommendCutRate > 0.05 ? true : false;
		});
	}, function(err, code){
		console.log(err + "["+ code +"]");
	});
	
	/* 刷新列表 */
	function refreshData(pageNo) {
		var param = {
			pageNo: pageNo,
			pageCount: 10,
		};
		AQ.callAQ("getRecommendProfit.do", param, function(result) {
			var data = [];
			for (var i=1; i<result.length; i++) {
				data.push(result[i]);
			}
			if(data.length == 0) {
				data.push({
					createTime: "您没有推荐用户哦！",
				});
			}
			$scope.$apply(function () {
				$scope.listData = data;
				if(pageNo == 1 && needNewPagination) {	//创建分页
					var num_entries = result[0].count;
					Utils.newPagination("Pagination", num_entries, function(page_index, jq) {
						if(page_index == 0 && needNewPagination) {
							needNewPagination = false;
							return;
						}
						refreshData(page_index+1);
					});
				}
			});
		}, function(err, code) {
			Utils.msgDialog(err + "["+ code +"]");
		});
	}
	
	AQ.callAQ("getRecommendId.do", null, function(result) {
		$("#tg-link").val(result[1].tinyurl);
		setTimeout(function() {
			Utils.appendZclip($("#copy-tg-bt")[0], $("#tg-link")[0]);
		}, 1000);
	}, function(err, code) {
		Utils.msgDialog(err + "["+ code +"]");
	});
	
	
}]);
